/*
 * 
 * Copyright 2011 visual-analysis-of-cancer.
 * This file is part of visual-analysis-of-cancer.
 * 
 * visual-analysis-of-cancer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * visual-analysis-of-cancer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * visual-analysis-of-cancer:
 * An tool to analye the WHO and GLOBOCAN cancer data.
 * http://code.google.com/p/visual-analysis-of-cancer/
 * 
 * 
 * 
 * Authors: Erik Sniegula, Johannes Dahlke
 * 
 * Module:  Programming in Processing for Visual Analytics
 * Group:   20
 * 
 * Created: Aug. 2011
 * Version: 1.0
 *
 */
 




package de.fuberlin.processing.who.cancer.gui.scatterplot;

import java.util.Collection;
import java.util.Vector;

import controlP5.ControlP5;
import controlP5.ControllerGroup;
import de.fuberlin.processing.who.cancer.data.structs.Struct_16_CancerIncidenceToAge;
import de.fuberlin.processing.who.cancer.interaction.LocationBroadcaster;
import de.fuberlin.processing.who.cancer.interaction.LocationModifier;
import processing.core.PApplet;


public class Cell_16_CancerIncidenceToAge extends CustomCell{

	public Cell_16_CancerIncidenceToAge( ControlP5 theControlP5,
			ControllerGroup theParent, String theName,
			CellConstructionAndProcessingParcel cellConstructionAndProcessingParcel,
			Collection coll, float theX, float theY, int theWidth, int theHeight) {
		super( theControlP5, theParent, theName, cellConstructionAndProcessingParcel,
				coll, theX, theY, theWidth, theHeight);
		this.locationModifier = LocationBroadcaster.get();
	}

	private LocationModifier locationModifier;
	
	@Override
	public void drawCell(PApplet p) {
	
		Vector<Struct_16_CancerIncidenceToAge> data = (Vector<Struct_16_CancerIncidenceToAge>) coll;
		

		float maxCancerIncidenceInAgeClass_0_14 = 0;
		float maxCancerIncidenceInAgeClass_15_39 = 0;
		float maxCancerIncidenceInAgeClass_40_44 = 0;
		float maxCancerIncidenceInAgeClass_45_49 = 0;
		float maxCancerIncidenceInAgeClass_50_54 = 0;
		float maxCancerIncidenceInAgeClass_55_59 = 0;
		float maxCancerIncidenceInAgeClass_60_64 = 0;
		float maxCancerIncidenceInAgeClass_65_69 = 0;
		float maxCancerIncidenceInAgeClass_70_74 = 0;
		float maxCancerIncidenceInAgeClass_75plus = 0;

		float minCancerIncidenceInAgeClass_0_14 = 0;
		float minCancerIncidenceInAgeClass_15_39 = 0;
		float minCancerIncidenceInAgeClass_40_44 = 0;
		float minCancerIncidenceInAgeClass_45_49 = 0;
		float minCancerIncidenceInAgeClass_50_54 = 0;
		float minCancerIncidenceInAgeClass_55_59 = 0;
		float minCancerIncidenceInAgeClass_60_64 = 0;
		float minCancerIncidenceInAgeClass_65_69 = 0;
		float minCancerIncidenceInAgeClass_70_74 = 0;
		float minCancerIncidenceInAgeClass_75plus = 0;

		for (Struct_16_CancerIncidenceToAge st : data) {
			if (st.cancerIncidenceInAgeClass_0_14 > maxCancerIncidenceInAgeClass_0_14)
				maxCancerIncidenceInAgeClass_0_14 = st.cancerIncidenceInAgeClass_0_14;
			
			if (st.cancerIncidenceInAgeClass_15_39 > maxCancerIncidenceInAgeClass_15_39)
				maxCancerIncidenceInAgeClass_15_39 = st.cancerIncidenceInAgeClass_15_39;
			
			if (st.cancerIncidenceInAgeClass_40_44 > maxCancerIncidenceInAgeClass_40_44)
				maxCancerIncidenceInAgeClass_40_44 = st.cancerIncidenceInAgeClass_40_44;
			
			if (st.cancerIncidenceInAgeClass_45_49 > maxCancerIncidenceInAgeClass_45_49)
				maxCancerIncidenceInAgeClass_45_49 = st.cancerIncidenceInAgeClass_45_49;
			
			if (st.cancerIncidenceInAgeClass_50_54 > maxCancerIncidenceInAgeClass_50_54)
				maxCancerIncidenceInAgeClass_50_54 = st.cancerIncidenceInAgeClass_50_54;
			
			if (st.cancerIncidenceInAgeClass_55_59 > maxCancerIncidenceInAgeClass_55_59)
				maxCancerIncidenceInAgeClass_55_59 = st.cancerIncidenceInAgeClass_55_59;
			
			if (st.cancerIncidenceInAgeClass_60_64 > maxCancerIncidenceInAgeClass_60_64)
				maxCancerIncidenceInAgeClass_60_64 = st.cancerIncidenceInAgeClass_60_64;
			
			if (st.cancerIncidenceInAgeClass_65_69 > maxCancerIncidenceInAgeClass_65_69)
				maxCancerIncidenceInAgeClass_65_69 = st.cancerIncidenceInAgeClass_65_69;
			
			if (st.cancerIncidenceInAgeClass_70_74 > maxCancerIncidenceInAgeClass_70_74)
				maxCancerIncidenceInAgeClass_70_74 = st.cancerIncidenceInAgeClass_70_74;
			
			if (st.cancerIncidenceInAgeClass_75plus > maxCancerIncidenceInAgeClass_75plus)
				maxCancerIncidenceInAgeClass_75plus = st.cancerIncidenceInAgeClass_75plus;
			
			
			if (st.cancerIncidenceInAgeClass_0_14 < minCancerIncidenceInAgeClass_0_14)
				minCancerIncidenceInAgeClass_0_14 = st.cancerIncidenceInAgeClass_0_14;
			
			if (st.cancerIncidenceInAgeClass_15_39 < minCancerIncidenceInAgeClass_15_39)
				minCancerIncidenceInAgeClass_15_39 = st.cancerIncidenceInAgeClass_15_39;
			
			if (st.cancerIncidenceInAgeClass_40_44 < minCancerIncidenceInAgeClass_40_44)
				minCancerIncidenceInAgeClass_40_44 = st.cancerIncidenceInAgeClass_40_44;
			
			if (st.cancerIncidenceInAgeClass_45_49 < minCancerIncidenceInAgeClass_45_49)
				minCancerIncidenceInAgeClass_45_49 = st.cancerIncidenceInAgeClass_45_49;
			
			if (st.cancerIncidenceInAgeClass_50_54 < minCancerIncidenceInAgeClass_50_54)
				minCancerIncidenceInAgeClass_50_54 = st.cancerIncidenceInAgeClass_50_54;
			
			if (st.cancerIncidenceInAgeClass_55_59 < minCancerIncidenceInAgeClass_55_59)
				minCancerIncidenceInAgeClass_55_59 = st.cancerIncidenceInAgeClass_55_59;
			
			if (st.cancerIncidenceInAgeClass_60_64 < minCancerIncidenceInAgeClass_60_64)
				minCancerIncidenceInAgeClass_60_64 = st.cancerIncidenceInAgeClass_60_64;
			
			if (st.cancerIncidenceInAgeClass_65_69 < minCancerIncidenceInAgeClass_65_69)
				minCancerIncidenceInAgeClass_65_69 = st.cancerIncidenceInAgeClass_65_69;
			
			if (st.cancerIncidenceInAgeClass_70_74 < minCancerIncidenceInAgeClass_70_74)
				minCancerIncidenceInAgeClass_70_74 = st.cancerIncidenceInAgeClass_70_74;
			
			if (st.cancerIncidenceInAgeClass_75plus < minCancerIncidenceInAgeClass_75plus)
				minCancerIncidenceInAgeClass_75plus = st.cancerIncidenceInAgeClass_75plus;
			
		}

		float m1 = 0;
		float m_y = 0;
		float m_x = 0;

		Collection<Integer> selectedLocations = locationModifier.getLocations();
		
		p.pushStyle();

		for (Struct_16_CancerIncidenceToAge st : data) {
			p.noFill();
			p.beginShape();
			
			if(selectedLocations.contains(st.locationId)){
				p.strokeWeight(STROKEWEIGHT_SELECTED);
				p.stroke(COLOR_SELECTED, STROKE_ALPHA_SELECTED);
			}
			else{
				p.strokeWeight(STROKEWEIGHT_NORMAL);
				p.stroke(COLOR_NORMAL, STROKE_ALPHA_NORMAL);
			}
      
			m_x = p.map(0, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerIncidenceInAgeClass_0_14, minCancerIncidenceInAgeClass_0_14, maxCancerIncidenceInAgeClass_0_14, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(1, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerIncidenceInAgeClass_15_39, minCancerIncidenceInAgeClass_15_39, maxCancerIncidenceInAgeClass_15_39, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(2, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerIncidenceInAgeClass_40_44, minCancerIncidenceInAgeClass_40_44, maxCancerIncidenceInAgeClass_40_44, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(3, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerIncidenceInAgeClass_45_49, minCancerIncidenceInAgeClass_45_49, maxCancerIncidenceInAgeClass_45_49, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(4, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerIncidenceInAgeClass_50_54, minCancerIncidenceInAgeClass_50_54, maxCancerIncidenceInAgeClass_50_54, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(5, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerIncidenceInAgeClass_55_59, minCancerIncidenceInAgeClass_55_59, maxCancerIncidenceInAgeClass_55_59, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(6, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerIncidenceInAgeClass_60_64, minCancerIncidenceInAgeClass_60_64, maxCancerIncidenceInAgeClass_60_64, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(7, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerIncidenceInAgeClass_65_69, minCancerIncidenceInAgeClass_65_69, maxCancerIncidenceInAgeClass_65_69, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(8, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerIncidenceInAgeClass_70_74, minCancerIncidenceInAgeClass_70_74, maxCancerIncidenceInAgeClass_70_74, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			
			m_x = p.map(9, 0, 9, 0, (int) width);
			m_y = p.map(st.cancerIncidenceInAgeClass_75plus, minCancerIncidenceInAgeClass_75plus, maxCancerIncidenceInAgeClass_75plus, 0,
					(int) height);
			p.vertex(left + m_x, top + height - m_y);
			p.endShape();
			p.strokeWeight(1);
			p.stroke(0);
		}
		p.popStyle();
		
	}

}
